package com.norteksoft.erm.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 角色
 * 
 * @hibernate.class table="ERM_ROLE"
 * 
 */
public class Role extends BaseObject {

	private static final long serialVersionUID = 3887909443356751179L;

	public static final String SPECIAL_TYPES_OUTER = "outer";// 匿名用户自动持有的特殊角色

	public static final String SPECIAL_TYPES_INNER = "inner";// 全体内部用户默认持有的特殊角色

	public static final String SPECIAL_TYPES_MANAGE_DOMAIN = "mdomain";// 管理域内用户默认持有的特殊角色

	public static final String MANAGE_DOMAIN_ALLUSER_POSTFIX = "_ALLUSER";// 管理域内用户默认持有的特殊角色

	public static final int ROLE_CAT_GLOBAL = 0;// 全局角色类别

	public static final int ROLE_CAT_DOMAIN = 1;// 管理域角色类别

	public static final int ROLE_CAT_CATEGORY = 2;// 自定义角色分类类别
	
	/**
	 * 三权分立--系统安全管理员角色code
	 */
	public static final String CHECKSAND_BALANCE_CODE_SYSSEC = "sysSec";
	
	/**
	 * 三权分立--系统监督日志管理员角色code
	 */
	public static final String CHECKSAND_BALANCE_CODE_SYSAUDIT = "sysAudit";
	
	/**
	 * 三权分立--系统维护管理员角色code
	 */
	public static final String CHECKSAND_BALANCE_CODE_SYSADMIN = "sysAdmin";

	private Long roleID = new Long(-1);// OID

	private String roleName;// 角色名称

	private String memo;// 备注

	private List users;// 角色用户集

	private List rights;// 角色权限集（norteksoft工作流专用）

	// add by Jiffy
	private String code;// 角色编号（必须唯一）

	private Group manageDomain;// 所属管理域

	private RoleCategory roleCategory;

	private String specialType;// 特殊角色类型（为空时，是一般角色）

	// ~~ deprecated ================================================

	private Role parentRole;// 父角色(角色类型)

	private Set childRoles = new HashSet();// 子角色集

	public int hashCode() {
		return new HashCodeBuilder().append(roleID).append(roleName).append(
				memo).append(code).append(specialType).toHashCode();
	}

	public Role() {
		super();
	}

	/**
	 * @hibernate.id generator-class="sequence" column="ROLEID"
	 *               unsaved-value="-1" type="long"
	 * @hibernate.generator-param name="sequence" value="ERM_SEQUENCE"
	 */

	public Long getRoleID() {
		return roleID;
	}

	public void setRoleID(Long roleID) {
		this.roleID = roleID;
	}

	/**
	 * @hibernate.property column="ROLENAME" length="255" type="string"
	 *                     not-null="true"
	 * @return String
	 */
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	/**
	 * @hibernate.property column="MEMO" length="2000" type="string"
	 * @return String
	 */

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * @hibernate.bag name="users" table="ERM_USER_ROLE" cascade="save-update"
	 *                lazy="true" inverse="true" outer-join="auto"
	 * @hibernate.key column="ROLEID"
	 * @hibernate.many-to-many class="com.norteksoft.erm.model.User"
	 *                         column="USERID"
	 * @return List
	 */
	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}

	/**
	 * @hibernate.bag name="rights" cascade="all-delete-orphan" lazy="true"
	 *                inverse="true"
	 * @hibernate.key column="ROLEID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.RoleRight"
	 * @return List
	 */
	public List getRights() {
		return rights;
	}

	public void setRights(List rights) {
		this.rights = rights;
	}

	/**
	 * @hibernate.property column="CODE" length="30" type="string"
	 *                     not-null="true"
	 * @return String
	 */
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @hibernate.many-to-one name="parentRole" column="PARENTID"
	 *                        class="com.norteksoft.erm.model.Role"
	 *                        outer-join="auto" cascade="none" outer-join="auto"
	 *                        update="true" insert="true"
	 */
	public Role getParentRole() {
		return parentRole;
	}

	public void setParentRole(Role parentRole) {
		this.parentRole = parentRole;
	}

	/**
	 * @hibernate.set name="childRoles" lazy="true" inverse="true" cascade="all"
	 * @hibernate.key column="PARENTID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.Role"
	 * @return Set
	 */
	public Set getChildRoles() {
		return childRoles;
	}

	public void setChildRoles(Set childRoles) {
		this.childRoles = childRoles;
	}

	/**
	 * @hibernate.many-to-one column="GROUPID" cascade="save-update"
	 *                        class="com.norteksoft.erm.model.Group"
	 *                        name="manageDomain"
	 * @return
	 */
	public Group getManageDomain() {
		return manageDomain;
	}

	public void setManageDomain(Group manageDomain) {
		this.manageDomain = manageDomain;
	}

	/**
	 * @hibernate.property column="SPECIALTYPE" length="10" type="string"
	 * @return String
	 */
	public String getSpecialType() {
		return specialType;
	}

	public void setSpecialType(String specialType) {
		this.specialType = specialType;
	}

	/**
	 * @hibernate.many-to-one column="ROLECATEGORYOID" cascade="save-update"
	 *                        class="com.norteksoft.erm.model.RoleCategory"
	 *                        name="roleCategory"
	 * @return
	 */

	public RoleCategory getRoleCategory() {
		return roleCategory;
	}

	public void setRoleCategory(RoleCategory roleCategory) {
		this.roleCategory = roleCategory;
	}

	// 特殊角色不可分配给用户
	public boolean getCanAssignToUser() {
		return StringUtils.isBlank(specialType);
	}
	
	/**
	 * 判断是否可以删除,如特殊角色,三权分立角色,部门领导,不能删除
	 */
	public boolean isRemoveabled(){
		if( StringUtils.isNotBlank(this.specialType)
				|| this.code.equalsIgnoreCase(CHECKSAND_BALANCE_CODE_SYSSEC)
				|| this.code.equalsIgnoreCase(CHECKSAND_BALANCE_CODE_SYSAUDIT)
				|| this.code.equalsIgnoreCase(CHECKSAND_BALANCE_CODE_SYSADMIN)){
			return false;
		}
		return true;
	}
	
	/**
	 * 获得三权分立角色code集合的json字符串
	 * @return
	 */
	public static String getCBRoleCodesJson(){
		
		return "".toString();
	}
	
	/**
	 * 获得三权分立的角色列表
	 * @return
	 */
	public static Set getCBRoleCodes(){
		Set set = new HashSet();
		set.add(CHECKSAND_BALANCE_CODE_SYSSEC);
		set.add(CHECKSAND_BALANCE_CODE_SYSAUDIT);
		set.add(CHECKSAND_BALANCE_CODE_SYSADMIN);
		return set;
	}
}
